import GameObject from "../GameObject";
import Utils from "../utils";
import BonusBombSprite from "./BonusBombSprite";
import GameConst from "../GameConst";

export default class BonusLineSprite extends GameObject{
    constructor(type, x, y) {
        super();
        this.sprites = [];
        this.speeds = [];
        this.type = type;
        var num;
        var rotaions;
        var potions;
        switch (type) {
        case 1:
            num = 2;
            rotaions = [90, -90];
            potions = [Utils.cjp(0, 0), Utils.cjp(0, 0)];
            break;
        case 2:
            num = 2;
            rotaions = [0, 180];
            potions = [Utils.cjp(0, 0), Utils.cjp(0, 0)];
            break;
        case 5:
            num = 4;
            rotaions = [90, -90, 0, 180];
            potions = [Utils.cjp(0, 0), Utils.cjp(0, 0), Utils.cjp(0, 0), Utils.cjp(0, 0)];
            break;
        case 6:
            num = 12;
            rotaions = [0, 0, 0, 90, 90, 90, 180, 180, 180, -90, -90, -90];
            potions = [Utils.cjp(-1, -1), Utils.cjp(0, -1), Utils.cjp(1, -1), Utils.cjp(1, -1), Utils.cjp(1, 0), Utils.cjp(1, 1), Utils.cjp(-1, 1), Utils.cjp(0, 1), Utils.cjp(1, 1), Utils.cjp(-1, -1), Utils.cjp(-1, 0), Utils.cjp(-1, 1)];
            break
        }
        var speed = GameConst.TILE_SIZE / GameConst.GEM_KILL_DELAY;
        for (var i = 0; i < num; ++i) {
            var image = Utils.createBitmap("explosion_line");
            image.x = x + potions[i].x * GameConst.TILE_SIZE;
            image.y = y + potions[i].y * GameConst.TILE_SIZE;
            this.sprites.push(image);
            Utils.addChild(image, GameConst.level.bonusLayer);
            image.pivotX = 25;
            image.pivotY = 65;
            image.rotation = rotaions[i];
            this.speeds.push(Utils.cjp(image.rotation == 90 ? speed : image.rotation == -90 ? -speed : 0, image.rotation == 0 ? -speed : image.rotation == 180 ? speed : 0));
        }
        GameConst.level.objects.push(this);
    }

    update(time) {
        var t = 0;
        var n = this.sprites.length;
        for (var i = 0; i < n; ++i) {
            var sprite = this.sprites[i];
            if (sprite.visible) {
                t++;
                sprite.x += this.speeds[i].x * time;
                sprite.y += this.speeds[i].y * time;
                var s = sprite.getBounds();
                if(s.width<1){
                    return;
                }
                if (s.x > GameConst.SCREEN_W + 100 || s.y + s.height > GameConst.ACTUAL_H + 100 || s.x  < -100 || s.y < GameConst.SHIFT_H) {
                    sprite.visible = false;
                    Utils.removeClip(sprite);
                }
            }
        }
        if (t == 0)
            this.destroy();
    };
}
var t = BonusBombSprite;
    
    t.prototype.destroy = function () {
        if (this.isDestroyed || !this.sprites)
            return;
        e.prototype.destroy.call(this);
        var t = this.sprites.length;
        for (var n = 0; n < t; ++n){
            Utils.removeClip(this.sprites[n]);
        }
        this.sprites = null;
        this.speeds = null;
    };
